package modeloredesdecomputadores;


import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HostClient {
    //IP do Host
    public static String ipv4 = "";
    //Nome do Host
    public static String hostname = "";
    //Nome do roteador ligado a este host
    public static String gateway = "";
    //IP do servidor de nomes
    private static String nameserver = "127.0.0.1";
    //Mensagem que será encaminhada
    public static String mensagem = "Olá!";

    public static void setGateway(String gateway) {
        HostClient.gateway = gateway;
    }
    
    private static void imprimeMenu() throws RemoteException {
        int opcao = 0;
                Scanner sn = new Scanner(System.in);
                do {
                        System.out.println("Menu:");
                        System.out.println("1 - Mandar mensagem");
                        System.out.println("2 - Definir gateway");
                        System.out.println("0 - Sair");
                        opcao = sn.nextInt();
                        switch (opcao) {
                        case 1:
                                System.out.println("Digite o nome do destino: ");
                                String destino = sn.next();
                                sendMsg(destino, mensagem);
                                break;
                        case 2:
                                System.out.println("Digite o nome do Gateway: ");
                                String gw = sn.next();
                                HostClient.setGateway(gw);
                                break;
                        default:
                                System.exit(0);
                                break;
                    }
                } while (opcao!=0);
        }

    private static void sendMsg(String ipdest, String msg) {
        try {
            Router r = (Router) Naming.lookup("rmi://"+nameserver+"/"+gateway);
            r.foward(ipdest,msg);
        } catch (NotBoundException ex) {
            Logger.getLogger(HostClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(HostClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(HostClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void getMsg(String msg){
        System.out.println(msg);
    }

    public HostClient(String iface) {
           
    }

    public static void main(String args[]){
                new HostClient(args[0]);
                
                hostname = args[0];
                ipv4 = args[1];
                
                try {
                        Host host = new HostImpl();
                        Naming.rebind("rmi://"+ipv4+"/"+hostname, host);
                        System.out.println("Host "+hostname+" iniciado.");
                        NameServer ns = (NameServer) Naming.lookup("rmi://"+nameserver+"/nameserver");
                        System.out.println(ns.registerHost(hostname, ipv4));
                        imprimeMenu();
                } catch(Exception e){
                        System.out.println("Exception: "+e);
                }
        }

}
